package com.houtai.common.util;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class CommonUtil {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat sdf_day = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat sdf_month = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat sdf_year = new SimpleDateFormat("yyyy");
    /**
     * @Purpose
     * @param
     * @author   lizhun
     * @version  1.0
     * @return   Date
     */
    public static String getMonthCalculate(int month)  throws ParseException {
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        int months = c.get(Calendar.MONTH);
        c.set(Calendar.MONTH,months + month);
        return sdf_month.format(c.getTime());

    }
    public static void main(String[] args)
    {
        String dateStr = "2019-06-17 1:21:28";
        String dateStr2 = "2019-06-18 2:21:28";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date date2 = format.parse(dateStr2);
            Date date = format.parse(dateStr);

            System.out.println("两个日期的差距：" + differentDays(date,date2));

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }




    /**
     * @Purpose
     * @param
     * @author   lizhun
     * @version  1.0
     * @return   Date
     */
    public static Date getDayStart(int day1)  throws ParseException {
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE,day + day1);
        String d = sdf_day.format(c.getTime()) + "000000";
        return sdf.parse(d);

    }
    /**
     * @Purpose
     * @param
     * @author   lizhun
     * @version  1.0
     * @return   Date
     */
    public static Date getDayEnd(int day1)  throws ParseException {
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE,day + day1);
        String d = sdf_day.format(c.getTime()) + "235959";
        return sdf.parse(d);

    }
    /**
     * @Purpose  天数计算
     * @param    days
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static Date datePlus2(int days, Date date) throws ParseException{
        Date now = new Date();
        if (date.compareTo(now) > 0) {
            now = date;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, days);
        String result = sdf_day.format(cal.getTime()) + "235959";
        return sdf.parse(result);
    }
    /**
     * @Purpose
     * @param
     * @author   lizhun
     * @version  1.0
     * @return   Date
     */
    public static Date getDayEnd1(int day1, Date date)  throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE,day + day1);
        String d = sdf_day.format(c.getTime()) + "235959";
        return sdf.parse(d);

    }
    /**
     * @Purpose  获取编号
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getOrderNub() {
        Date date = new Date();
        String orderNub = sdf.format(date).substring(2, 12);

        String code = "";
        while(code.length() < 7)
        {
            code += (int)(Math.random() * 10);
        }
        orderNub = orderNub + code;
        return orderNub;
    }
    /**
     * @Purpose  获取年月日
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getDay() {
        Date date = new Date();
        return sdf_day.format(date);
    }
    /**
     * @Purpose  获取年月
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getMonth() {
        Date date = new Date();
        return sdf_month.format(date);
    }
    /**
     * @Purpose  获取年
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getYear() {
        Date date = new Date();
        return sdf_year.format(date);
    }
    /**
     * @Purpose  生成token
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getToken() {
        UUID token = UUID.randomUUID();
        return "" + token;
    }
    /**
     * @Purpose  天数计算
     * @param    days
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String datePlus(int days) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, days);
        return sdf_day.format(cal.getTime());
    }
    /**
     * @Purpose   生成分页信息
     * @param
     * @version   1.0
     * @author    lizhun
     * @time      2017/6/9 0009 下午 4:11
     * @return    Map
     */
    public static Map<String, Object> page(int total_record , int current_page , int page_size){
        int total_page;
        if (total_record % page_size == 0) {
            total_page = total_record/page_size;
        } else {
            total_page = (total_record/page_size) + 1;
        }
        if (current_page >= total_page) {
            current_page = total_page;
        }
        if (current_page <= 1) {
            current_page = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("current_page", (current_page - 1) * page_size);
        map.put("page_size", page_size);
        map.put("total_record", total_record);
        map.put("total_page",total_page);
        return map;
    }
}
